import { Injectable } from '@angular/core';

import { HttpClient,HttpErrorResponse } from '@angular/common/http';
import { catchError, retry } from 'rxjs/operators';

import { NgxhttpService } from '../../@core/utils/ngxhttp.service';
import { Router } from '@angular/router';
import { NbTokenService } from '@nebular/auth';
import { Observable, throwError } from 'rxjs';

export class CcqInfo {
  stcd: string;
  name: string;
  z:string;
  ms:string;
  type:string;
  unit:string;
  stlc:string;
}

export class StInfo {
  stcd: string;
  name: string;
  stType:string;
  typeNm:string;
  x:string;
  y:string;

}

@Injectable()
export class DynamicsService extends NgxhttpService{
  configUrl = 'assets/pages/dynamics/config.json';

  constructor(private http: HttpClient,
              tokenService: NbTokenService,
              router : Router) {
    super(tokenService, router);
  }

  queryCcqInfo(type): Observable<CcqInfo[]>{
    if(type=="1"){
      this.configUrl = 'assets/pages/dynamics/config.json';
    }
    if(type=="2"){
      this.configUrl = 'assets/pages/dynamics/config2.json';
    }
    return this.http.get<CcqInfo[]>(this.configUrl)
      .pipe(
        retry(3), // retry a failed request up to 3 times
        catchError((error: HttpErrorResponse) =>
          this.handleError(error)
        ) // then handle the error
      );
  }

  queryStInfo(): Observable<StInfo[]>{
    this.configUrl = 'assets/pages/dynamics/config3.json';
    return this.http.get<StInfo[]>(this.configUrl)
      .pipe(
        retry(3), // retry a failed request up to 3 times
        catchError((error: HttpErrorResponse) =>
          this.handleError(error)
        ) // then handle the error
      );
  }

  /*  handleError(error: HttpErrorResponse) {
      var tt = this;
      return throwError(
        'Something bad happened; please try again later.');

    }*/
}
